package com.lwhtarena.netty.netty4.util;

import com.clickhouse.jdbc.ClickHouseDataSource;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDateTime;
import java.util.Properties;

public class ClickHouse {


    public static void init() throws SQLException {
        String url = "jdbc:ch://192.168.2.13:8123"; // use http protocol and port 8123 by default

        Properties properties = new Properties();

        ClickHouseDataSource dataSource = new ClickHouseDataSource(url, properties);

        try (
                Connection conn = dataSource.getConnection("default", "");
                Statement stmt = conn.createStatement()
        ) {
            ResultSet resultSet = stmt.executeQuery("select * from ckweight.sys_record_dimension");
            while (resultSet.next()) {
                //null 会转换成默认值
                System.out.print(resultSet.getString(1)+"\t");
                System.out.print(resultSet.getString(2)+"\t");
                System.out.print(resultSet.getString(3)+"\t");
                LocalDateTime checkTime = (LocalDateTime) resultSet.getObject("check_time");
                System.out.println(checkTime);
            }
        }
    }

    public static void main(String[] args) {
        try{
            ClickHouse.init();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
